package com.baoleme.controller;


import com.baoleme.common.Result;

import com.baoleme.dto.EmployeeLoginDTO;
import com.baoleme.dto.EmployeeModifyPasswordDTO;
import com.baoleme.dto.EmployeePageQueryDTO;
import com.baoleme.entity.Employee;
import com.baoleme.exceptions.BusinessException;
import com.baoleme.exceptions.enumeration.ResponseEnum;
import com.baoleme.service.EmployeeService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2024-09-13
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    EmployeeService employeeService;


    @Operation(summary = "员工登录")
    @PostMapping("login")
    //登录方法
    public Result<Map<String, String>> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        Map<String, String> tokens = employeeService.login(employeeLoginDTO);
        return Result.success(tokens);
    }

    @Operation(summary = "退出登录")
    @GetMapping("logout")
    public Result<Boolean> logout(String username){
        boolean flag = employeeService.logout(username);
        return Result.success(flag);
    }

    //修改密码
    @Operation(summary = "修改密码")
    @PutMapping("modifyPassword")
    public Result<Boolean> modifyPassword(@RequestBody EmployeeModifyPasswordDTO employeeModifyPasswordDTO){

          boolean flag=employeeService.modifyPassword(employeeModifyPasswordDTO);

          return Result.success(flag);
    }
    //根据员工id查询信息
    @Operation(summary = "根据员工id查询信息")
    @GetMapping("{id}")
    public Result<Employee> queryEmployeeById(@PathVariable("id") String id){
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }


    //新增员工
    @Operation(summary = "新增员工")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Employee employee){

        boolean flag = employeeService.saveEmployee(employee);
        return Result.success(flag);
    }


    @Operation(summary = "禁用账号")
    @PostMapping("status")
    public Result<Boolean> status(@RequestBody Employee employee){

        boolean flag = employeeService.status(employee);

        return Result.success(flag);
    }

    @Operation(summary = "修改员工信息")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Employee employee){

        boolean flag = employeeService.updateById(employee);

        return Result.success(flag);
    }

    @Operation(summary = "删除员工")
    @DeleteMapping("delete")
    public Result<Boolean> delete(@RequestBody Employee employee){

        boolean flag = employeeService.removeById(employee);

        return Result.success(flag);
    }

    @Operation(summary = "分页查询员工信息")
    @PostMapping("page")
    public IPage<Employee> page(@RequestBody EmployeePageQueryDTO employeePageQueryDTO){
        if (employeePageQueryDTO==null){
            throw new BusinessException(ResponseEnum.ILLEGAL_OPERATION);
        }
        return employeeService.getPage(employeePageQueryDTO);
    }



}
